package person.bing.fbcloud.common.security.util;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 获取用户信息工具类
 * @author: mengfanbing
 * @date: 2020/2/28 17:10
 */
public class UserUtil {
    /**
     * 获取用户名称
     * @return
     */
    public static String getUserName(){
        String username = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            username = ((UserDetails)principal).getUsername();
        } else {
            username = principal.toString();
        }
        return username;
    }
}
